<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工程名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入工程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建设单位" prop="constructEnterprise">
        <el-input
          v-model="queryParams.constructEnterprise"
          placeholder="请输入建设单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中标时间">
        <el-date-picker
          v-model="daterangeBidDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>


      <!-- 以下为高级查询 -->
      <el-form-item label="中标金额" prop="bidAmount" v-show="showHighSearch">
        <el-input
          v-model="queryParams.bidAmount"
          placeholder="请输入中标金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="竣工时间" v-show="showHighSearch">
        <el-date-picker
          v-model="daterangeCompleteTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="项目经理" prop="projectManager" v-show="showHighSearch">
        <el-input
          v-model="queryParams.projectManager"
          placeholder="请输入项目经理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="success" icon="el-icon-search" size="mini" @click='showHighSearch=!showHighSearch'>高级搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:compete_enterprise_project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:compete_enterprise_project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:compete_enterprise_project:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:compete_enterprise_project:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="compete_enterprise_projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="竞争单位名称" align="left" header-align="center" prop="enterpriseName" width="210" fixed/>
      <el-table-column label="工程名称" align="left" header-align="center" prop="projectName" width="350" fixed/>
      <el-table-column label="建设单位" align="center" prop="constructEnterprise" />
      <el-table-column label="中标时间" align="center" prop="bidDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bidDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中标金额" align="center" prop="bidAmount" />
      <el-table-column label="竣工时间" align="center" prop="completeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目经理" align="center" prop="projectManager" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:compete_enterprise_project:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:compete_enterprise_project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改历史业绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="竞争单位名称" prop="enterpriseId"  style="width:320px;">
          <el-select
            v-model="form.enterpriseId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择竞争单位名称"
            :remote-method="getCompeteEnterpriseList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="工程名称" prop="projectName" style="width:330px;">
          <el-input v-model="form.projectName" placeholder="请输入工程名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="建设单位" prop="constructEnterprise" style="width:320px;">
          <el-input v-model="form.constructEnterprise" placeholder="请输入建设单位" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="中标时间" prop="bidDate">
          <el-date-picker clearable
            v-model="form.bidDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择中标时间">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="中标金额" prop="bidAmount" style="width:320px;">
          <el-input v-model="form.bidAmount" placeholder="请输入中标金额" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="竣工时间" prop="completeTime">
          <el-date-picker clearable
            v-model="form.completeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择竣工时间">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="项目经理" prop="projectManager" style="width:320px;">
          <el-input v-model="form.projectManager" placeholder="请输入项目经理" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">竞争单位历史项目获奖信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCompeteEnterpriseProjectAward">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCompeteEnterpriseProjectAward">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="competeEnterpriseProjectAwardList" :row-class-name="rowCompeteEnterpriseProjectAwardIndex" @selection-change="handleCompeteEnterpriseProjectAwardSelectionChange" ref="competeEnterpriseProjectAward">
          <el-table-column type="selection" width="50" align="center" />

          <el-table-column label="获奖等级" prop="awardGrade" width="110">
            <template slot-scope="scope">
              <el-input v-model="scope.row.awardGrade" placeholder="请输入获奖等级" />
            </template>
          </el-table-column>
          <el-table-column label="获奖名称" prop="awardName" width="110">
            <template slot-scope="scope">
              <el-input v-model="scope.row.awardName" placeholder="请输入获奖名称" />
            </template>
          </el-table-column>
          <el-table-column label="颁奖部门" prop="awardDepartment" width="110">
            <template slot-scope="scope">
              <el-input v-model="scope.row.awardDepartment" placeholder="请输入颁奖部门" />
            </template>
          </el-table-column>
          <el-table-column label="获奖日期" prop="awardDate" width="150">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.awardDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择获奖日期" />
            </template>
          </el-table-column>
          <el-table-column label="负责人" prop="leader" width="110">
            <template slot-scope="scope">
              <el-input v-model="scope.row.leader" placeholder="请输入负责人" />
            </template>
          </el-table-column>
          <el-table-column label="证书图片" prop="remark" width="350">
            <template slot-scope="scope">
              <image-upload style="width:350px;hight:65px" v-model="scope.row.image"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompete_enterprise_project, getCompete_enterprise_project, delCompete_enterprise_project, addCompete_enterprise_project, updateCompete_enterprise_project } from "@/api/business/compete_enterprise_project";
import { listCompete_enterprise} from "@/api/business/compete_enterprise";

export default {
  name: "Compete_enterprise_project",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCompeteEnterpriseProjectAward: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //高级搜索
      showHighSearch: false,
      // 总条数
      total: 0,
      // 历史业绩表格数据
      compete_enterprise_projectList: [],
      // 竞争单位历史项目获奖表格数据
      competeEnterpriseProjectAwardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeBidDate: [],
      // 备注时间范围
      daterangeCompleteTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        constructEnterprise: null,
        bidDate: null,
        bidAmount: null,
        completeTime: null,
        projectManager: null,
        enterpriseId: null,
      },
      options: [],
      //竞争企业查询参数
      queryEnterpriseParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
      },
      option: {
        enterpriseId: null,
        enterpriseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: "竞争单位名称不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "工程名称不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "工程名称不能为空", trigger: "blur" }
        ],
        constructEnterprise: [
          { required: true, message: "建设单位不能为空", trigger: "blur" }
        ],
        bidDate: [
          { required: true, message: "中标时间不能为空", trigger: "blur" }
        ],
        
        projectManager: [
          { required: true, message: "项目经理不能为空", trigger: "blur" }
        ],
        bidAmount: [
          { required: true, validator: (rule, value, callback) => {
          if (!value) {
            callback(new Error('金额不能为空'))
          } else {
            if (!this.isNumber(value)) {
              callback(new Error('请输入数字值'))
            } else {
              callback()
            }
          }
        }, trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const tempEnterpriseId = this.$route.params && this.$route.params.enterpriseId;
    if(tempEnterpriseId!=null){
      this.queryParams.enterpriseId = tempEnterpriseId;
    }
    this.getList();
  },
  methods: {
    /** 查询历史业绩列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBidDate && '' != this.daterangeBidDate) {
        this.queryParams.params["beginBidDate"] = this.daterangeBidDate[0];
        this.queryParams.params["endBidDate"] = this.daterangeBidDate[1];
      }
      if (null != this.daterangeCompleteTime && '' != this.daterangeCompleteTime) {
        this.queryParams.params["beginCompleteTime"] = this.daterangeCompleteTime[0];
        this.queryParams.params["endCompleteTime"] = this.daterangeCompleteTime[1];
      }
      listCompete_enterprise_project(this.queryParams).then(response => {
        this.compete_enterprise_projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    isNumber (val) {
      var regPos = /^\d+(\.\d+)?$/
      var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/
      if (regPos.test(val) || regNeg.test(val)) {
        return true
      } else {
        return false
      }
    },
    getCompeteEnterpriseList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryEnterpriseParams.enterpriseName = query;
        listCompete_enterprise(this.queryEnterpriseParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        enterpriseId: null,
        projectName: null,
        constructEnterprise: null,
        bidDate: null,
        bidAmount: null,
        completeTime: null,
        projectManager: null,
        creator: null,
        createTime: null,
        updateTime: null,
        options: [],
        remark: null
      };
      this.competeEnterpriseProjectAwardList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBidDate = [];
      this.daterangeCompleteTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加竞争单位历史业绩";
      this.loading = true;
      this.queryEnterpriseParams.enterpriseName = '';
      listCompete_enterprise(this.queryEnterpriseParams).then(response => {
        this.loading = false;
        this.options = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.option.enterpriseId=row.enterpriseId;
      this.option.enterpriseName=row.enterpriseName;
      this.options.push(this.option);
      const projectId = row.projectId || this.ids
      getCompete_enterprise_project(projectId).then(response => {
        this.form = response.data;
        this.competeEnterpriseProjectAwardList = response.data.competeEnterpriseProjectAwardList;
        this.open = true;
        this.title = "修改竞争单位历史业绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.competeEnterpriseProjectAwardList = this.competeEnterpriseProjectAwardList;
          if(this.competeEnterpriseProjectAwardList!=null){
            for (var j = this.competeEnterpriseProjectAwardList.length - 1; j >= 0; j--) {
              let tempAward = this.competeEnterpriseProjectAwardList[j];
              console.log(tempAward);
              if (tempAward.awardGrade==null|| tempAward.awardName==null|| tempAward.awardDepartment==null
              || tempAward.awardDate==null|| tempAward.leader==null|| tempAward.image==null
              ||tempAward.awardGrade==''|| tempAward.awardName==''|| tempAward.awardDepartment==''
              || tempAward.awardDate==''|| tempAward.leader==''|| tempAward.image=='') {
                alert("获奖信息字段都必填");
                return;
              }
            }
          }
          
          if (this.form.projectId != null) {
            updateCompete_enterprise_project(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompete_enterprise_project(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$modal.confirm('是否确认删除历史业绩？').then(function() {
        return delCompete_enterprise_project(projectIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 竞争单位历史项目获奖序号 */
    rowCompeteEnterpriseProjectAwardIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 竞争单位历史项目获奖添加按钮操作 */
    handleAddCompeteEnterpriseProjectAward() {
      let obj = {};
      obj.awardGrade = "";
      obj.awardName = "";
      obj.awardDepartment = "";
      obj.awardDate = "";
      obj.leader = "";
      obj.image = "";
      obj.remark = "";
      this.competeEnterpriseProjectAwardList.push(obj);
    },
    /** 竞争单位历史项目获奖删除按钮操作 */
    handleDeleteCompeteEnterpriseProjectAward() {
      if (this.checkedCompeteEnterpriseProjectAward.length == 0) {
        this.$modal.msgError("请先选择要删除的竞争单位历史项目获奖数据");
      } else {
        const competeEnterpriseProjectAwardList = this.competeEnterpriseProjectAwardList;
        const checkedCompeteEnterpriseProjectAward = this.checkedCompeteEnterpriseProjectAward;
        this.competeEnterpriseProjectAwardList = competeEnterpriseProjectAwardList.filter(function(item) {
          return checkedCompeteEnterpriseProjectAward.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCompeteEnterpriseProjectAwardSelectionChange(selection) {
      this.checkedCompeteEnterpriseProjectAward = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/compete_enterprise_project/export', {
        ...this.queryParams
      }, `compete_enterprise_project_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
